import { getUploadToken } from '@/api/user'
import dayjs from 'dayjs'
import { randomString } from '@/utils'

export default {
  defaultType: 'qiniu', // 默认的上传后端类型
  qiniu: {
    bucket: 'd2p-demo',
    getToken (custom, fileType) {
      return getUploadToken()
    },
    domain: 'https://pic.xishuw.com'
  },
  buildKey (fileName, custom, type) { // 文件key的构建规则
    const date = dayjs().format('YYYY-MM-DD')
    let fileType = type.substring(0, type.lastIndexOf('/')) || 'file'
    if (custom != null && custom.fileType != null) {
      fileType = custom.fileType
    }
    let ext = ''
    if (fileName.lastIndexOf('.') >= 0) {
      ext = fileName.substring(fileName.lastIndexOf('.'))
    }
    return 'xishu-door' + fileType + '/' + date + '/' + randomString(8) + ext
  }
}
